const Router = require('koa-router')
const User = require('../moudel/User')
const rt = new Router()

rt.post('/login',async (ctx)=>{
    let {user,pwd} = ctx.request.body
    let results = null
    results= await User.login(user,pwd)
    ctx.body = results
})

// 轮播图路由
rt.get('/banner',async (ctx)=>{
    let results = await User.Banner()
    // console.log('来过了');
    ctx.body = results
})

rt.get('/nav',async (ctx)=>{
    let results = await User.NavList()
    ctx.body = results
})

rt.get('/fier/:pid',async (ctx)=>{
    // 获取到地址后面携带的动态参数
    let pid = ctx.params.pid
    let results = await User.LogIc(pid)
    // console.log(results);
    ctx.body = results
})

rt.post('/register', async(ctx)=>{
    let {user,pwd,addre,oid} = ctx.request.body
    let results = await User.Register(user,pwd,addre,oid)
    if(results.affectedRows == 1){
        ctx.body = {code:'200',msg:'注册成功!'}
    }else{
        ctx.body = {code:'201',msg:'请检查用户名或者密码是否符合规范!'}
    }
    // console.log(user,pwd);
})

// 查询用户名是否存在
rt.get('/verify/:user',async(ctx)=>{
    let user = ctx.params.user
    let results = await User.Username(user)
    // ctx.body = results
    if(results.length>0){
        ctx.body = {code:'201',msg:'用户名已经存在!'}
    }else{
        ctx.body = {code:'200',msg:'用户名可用!'}
    }
})

// 添加用户对应的地址
rt.post('/address/add',async(ctx)=>{
    let data = ctx.request.body
    let results = await User.Addre(data)
    if(results.affectedRows == 1){
            ctx.body = {code:'200',msg:'添加成功!'}
        }else{
            ctx.body = {code:'201',msg:'添加失败!'}
        }
    // ctx.body = results
})

// 查询用户对应的地址

rt.get('/address/:id',async(ctx)=>{
    let id = ctx.params.id
    let results = await User.GetAddre(id)
    ctx.body = results
})

module.exports = rt